<?php
namespace Home\Controller;

use Home\Util\lib\YZTokenClient;
use Util\AccessController;
/*
 * 有赞接口
 */
class IndexController extends AccessController {

    public function index(){
        $this->display();
    }
    /*
     * 获取有赞所有上架的商品
     * 即更新数据
     */
    public function GetAllGoods(){
        $client = new YZTokenClient($this->S('access_token'));
        $method = 'youzan.items.onsale.get'; //要调用的api名称
        $api_version = '3.0.0'; //要调用的api版本号
        $page_size = 50;
        $my_params = [
            'order_by' => 'created_time:desc',
            'page_no' => 1,
            'page_size' => $page_size,
            'q' => '',
        ];
        $my_files = [
        ];
        $SJdata = $client->post($method, $api_version, $my_params, $my_files);
        $LS = Service('YZGoods','yz_goods');
        $ALLDATA = array();
        for($i= 1;$i < $SJdata['response']['count']/$page_size+1;$i++){
            $my_params = [
                'order_by' => 'created_time:desc',
                'page_no' => $i,
                'page_size' => $page_size,
                'q' => '',
            ];
            $SJdata = $client->post($method, $api_version, $my_params, $my_files);
            $ALLDATA[] =$SJdata['response']['items'];
        }
        $OK = $LS->AddAll($ALLDATA);
        if($OK){
            $this->redirect('Goods/ShowAllYZGoods');
        }else{
            $this->error("更新失败");
        }
    }


    /*
    * 获取待处理订单
    * 获取有赞订单
    * 即更新数据
    */
    public function GetAllOrder(){
        $client = new YZTokenClient($this->S('access_token'));
        $method = 'kdt.trades.sold.get';//要调用的api名称
        $api_version = '2.0.0';//要调用的api版本号
        $page_size = 10;
        $status = 'WAIT_SELLER_SEND_GOODS';
        if($_GET['status']){
            $status = $_GET['status'];
        }
        $where['status'] = $status;
        $my_params = array(
            'page_size'=>$page_size,
            'type' => 'PIFA',
            'status' =>$status,
        );
        $my_files = [
        ];
        $SJdata = $client->post($method, $api_version, $my_params, $my_files);
        $ALLdata = array();
        $ALLdataInfo = array();
        for($i= 1;$i < $SJdata['response']['total_results']/$page_size+1;$i++){
            $my_params = array(
                'page_size'=>$page_size,
                'type' => 'PIFA',
                'status' =>$status,
                'page_no'=>$i,
            );
            $SJdata = $client->post($method, $api_version, $my_params, $my_files);
            foreach($SJdata['response']['trades'] as $k => $v){
                $data['tid'] = $v['tid'];//交易编号
                $order = array();
                foreach($v['orders'] as $k1 => $v1){
                    $order[$k1] = $v1['item_id'];
                    $dataInfo['item_id'] =  $v1['item_id'];
                    $dataInfo['alias'] =  $v1['alias'];
                    $dataInfo['title'] =  $v1['title'];
                    $dataInfo['seller_nick'] =  $v1['seller_nick'];
                    $dataInfo['price'] =  $v1['price'];
                    $dataInfo['total_fee'] =  $v1['total_fee'];
                    $dataInfo['payment'] =  $v1['payment'];
                    $dataInfo['sku_properties_name'] =  $v1['sku_properties_name'];
                    $dataInfo['num'] =  $v1['num'];
                    $dataInfo['oid'] = $v['tid'];//交易编号
                    $ALLdataInfo[] = $dataInfo;
                }
                $data['payment'] = $v['payment'];//实付金额。单位：元，精确到分
                $data['total_fee'] = $v['total_fee'];//商品总价
                $data['receiver_name'] = $v['receiver_name'];//收货人的姓名
                $data['receiver_mobile'] = $v['receiver_mobile'];//收货人的手机号码
                $data['receiver_address'] = $v['receiver_address'];//收货人的详细地址
                $data['status'] = $v['status'];//交易状态。取值范围： WAIT_BUYER_PAY (等待买家付款) ； WAIT_GROUP（等待成团，即：买家已付款，等待成团）； WAIT_SELLER_SEND_GOODS (等待卖家发货，即：买家已付款) ； WAIT_BUYER_CONFIRM_GOODS (等待买家确认收货，即：卖家已发货) ； TRADE_BUYER_SIGNED (买家已签收) ； TRADE_CLOSED_BY_USER (付款以前，卖家或买家主动关闭交易) ； TRADE_CLOSED (付款以后用户退款成功，交易自动关闭)
                $data['created'] = $v['created'];//交易创建时间
                $ALLdata[] = $data;
            }
        }
        $this->S('OrderData',NULL);
        $this->S('OrderDataInfo',NULL);
        $this->S('OrderData',$ALLdata);
        $this->S('OrderDataInfo',$ALLdataInfo);

//        $this->redirect('Order/Index',array('status'=>$status));
        $this->redirect('Order/NCindex');
//        $LS = Service('YZOrder','yz_order');
//        $ALLOK = $LS->AddAll($ALLdata,$ALLdataInfo,$where);
//        if($ALLOK == 1){
//            $this->redirect('Order/Index',array('status'=>$status));
//        }
//        if($ALLOK == 2){
//            $this->error("无数据更新");
//        }
//        if($ALLOK == 0){
//            $this->error("更新失败");
//        }
    }


    /*
     *
     * 有赞订单发货
     *
     */

    public function Shipments(){
        $context = stream_context_create(array('http'=>array('ignore_errors'=>true)));
        $urlss = 'http://www.goonee.me/zhechaojson.index.php?confirm='.$_GET['tid'].'&token='.urlencode($this->S('access_token'));
        $content = file_get_contents($urlss, FALSE, $context);
        $orderlistass = json_decode($content,TURE);
        if(intval($orderlistass['response']['is_success'])==1) {
            $where['tid'] = $_GET['tid'];
            $save['status'] = 'WAIT_BUYER_CONFIRM_GOODS';
            $LS = Service('YZOrder','yz_order');
            $OK = $LS->update($where,$save);
            if($OK){
                redirect(U('Order/index'));
            }else{
                $this->error('平台数据更新失败');
            }
        }else{
            $this->error('平台数据更新失败');
        }

    }

    /*
     * 修改商品信息
     */
    public function UpdataGoods(){

    }
}